package com.lins.wechat.util;

import com.alibaba.fastjson.JSONObject;
import com.lins.wechat.config.WeChatConfigure;
import com.lins.wechat.pojo.Weather;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author linshuo
 * @Date 2023/7/14 15:37
 */
public class DataUtils {

    /**
     * 获取 Weather 信息
     * @param restTemplate
     * @return
     */
    public static Weather getWeather(RestTemplate restTemplate,String cityCode){
//        String responseJson = restTemplate.getForObject(WeChatConfigure.Weather_API, String.class);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("key", WeChatConfigure.Weather_KEY);
        paramMap.put("city", cityCode);
        paramMap.put("type", 1);
        String json = restTemplate.getForObject(WeChatConfigure.Weather_API, String.class, paramMap);

//        HttpHeaders headers = new HttpHeaders();
//        headers.setAccept(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED));
//        HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
//        ResponseEntity<String> result = restTemplate.exchange(WeChatConfigure.Weather_API, HttpMethod.GET, entity, String.class);

        /*String result = restTemplate.getForObject(LinsUtil.merge("{}?key={}&city={}&type={}",WeChatConfigure.Weather_API,WeChatConfigure.Weather_KEY,"101190201","1"),
                String.class);
        System.out.println("result1====================" + result);*/

////2、使用exchange请求接口
//        HttpHeaders headers = new HttpHeaders();
//        headers.set("id", "lidy");
//        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers);
//        ResponseEntity<String> response2 = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);



        //json转weather对象
//        String json = "{\"date\":\"2023-07-14\",\"week\":\"星期五\",\"province\":\"江苏\",\"area\":\"无锡\",\"areaid\":\"101190201\",\"weather\":\"多云\",\"weatherimg\":\"duoyun.png\",\"weathercode\":\"duoyun\",\"real\":\"36℃\",\"lowest\":\"27℃\",\"highest\":\"36℃\",\"wind\":\"西南风\",\"windspeed\":\"9\",\"windsc\":\"2级\",\"sunrise\":\"05:03\",\"sunset\":\"19:05\",\"moonrise\":\"\",\"moondown\":\"\",\"pcpn\":\"0\",\"uv_index\":\"0\",\"aqi\":\"50\",\"quality\":\"优\",\"vis\":\"19\",\"humidity\":\"55\",\"alarmlist\":[{\"province\":\"江苏省\",\"city\":\"无锡市\",\"level\":\"黄色\",\"type\":\"高温\",\"content\":\"无锡市气象台2023年07月14日09时10分继续发布高温黄色预警信号：预计今天下午我市大部分街道（镇）最高气温将达36℃左右，城区37℃左右，请注意防暑降温。（预警信息来源：国家预警信息发布中心）\",\"time\":\"2023-07-14 09:13:00\"}],\"tips\":\"天气极热，适宜着短衣、短裙、短裤等夏季服装。午后尽量减少户外活动，高温条件下作业和露天作业人员采取必要防护措施。空气质量优秀，快去户外呼吸清新空气吧。\"}";
        return JSONObject.parseObject(json,Weather.class);
    }

    /**
     * 获取 RainbowPi 信息
     * @param restTemplate
     * @return
     */
    public static String getRainbow(RestTemplate restTemplate){
        String responseJson = restTemplate.getForObject(WeChatConfigure.Rainbow_API, String.class);
        JSONObject responseResult = JSONObject.parseObject(responseJson);
        JSONObject jsonObject = responseResult.getJSONArray("newslist").getJSONObject(0);
        return jsonObject.getString("content");
    }

    /**
     * 计算生日天数 days
     * @return
     */
    public static int getBirthDays(String birthday) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cToday = Calendar.getInstance(); // 存今天
        Calendar cBirth = Calendar.getInstance(); // 存生日
        int days = 0;
        try {
            cBirth.setTime(dateFormat.parse(birthday)); // 设置生日
            cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年
            if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
                // 生日已经过了，要算明年的了
                days = (cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR)) + cBirth.get(Calendar.DAY_OF_YEAR);
            } else {
                // 生日还没过
                days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 计算恋爱天数 days
     * @return
     */
    public static int getLoveDays(String loveday){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        try {
            long time = System.currentTimeMillis() - dateFormat.parse(loveday).getTime();
            days = (int) (time / (24*60*60*1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }


    public static String getAccessToken(RestTemplate restTemplate){
        return "";
    }



}
